using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class SectorCreate : MonoBehaviour
{
	[Serializable]
	public class LightMapUV
	{
		public Vector4 lightmapScaleOffset;

		public int lightMapIndex;

		public Renderer renderer;
	}

	public bool lod;

	public bool resourcesLoaded;

	public bool LoadWithoutPause;

	public List<GameObject> listObj = new List<GameObject>();

	public TextureChanger[] arrTextureChangerDisabledForWeak;

	public TextureChanger[] arrTextureChanger;

	[SerializeField]
	public List<LightMapUV> lightMapUVs = new List<LightMapUV>();

	[HideInInspector]
	public bool createIsComplete;

	private SectorInfo curSectorInfo;

	private void Start()
	{
		if (Application.isPlaying)
		{
			if (lod && Device.indexWeakDevice != 0)
			{
				ClearMeshes();
				base.gameObject.SetActive(false);
			}
			else if (lod)
			{
				Init();
			}
		}
	}

	public void LoadRes()
	{
		StartCoroutine(MeshManager());
	}

	public void Init()
	{
		if (QualityController.LightEnable && !lod)
		{
			LoadAllUVs();
		}
		if (lod)
		{
			StartCoroutine(MeshManager());
		}
		else
		{
			base.gameObject.SetActive(false);
		}
	}

	private void LoadAllUVs()
	{
		for (int i = 0; i < lightMapUVs.Count; i++)
		{
			if (lightMapUVs[i].renderer != null)
			{
				lightMapUVs[i].renderer.lightmapIndex = lightMapUVs[i].lightMapIndex;
				lightMapUVs[i].renderer.lightmapScaleOffset = lightMapUVs[i].lightmapScaleOffset;
			}
		}
	}

	private IEnumerator MeshManager()
	{
		if (resourcesLoaded)
		{
			yield break;
		}
		if (BCWResourceManager.instance != null)
		{
			if (!lod && ManagerPreloadingSectors.thisScript.isFirstLoading && !ManagerPreloadingSectors.thisScript.listFirstLoadingSectors.Contains(this))
			{
				ManagerPreloadingSectors.thisScript.listFirstLoadingSectors.Add(this);
			}
			if (!lod)
			{
				if (ManagerPreloadingSectors.thisScript.isFirstLoading)
				{
					for (int j = 0; j < arrTextureChanger.Length; j++)
					{
						if (arrTextureChanger[j] != null && !arrTextureChanger[j].refCountIncreased)
						{
							StartCoroutine(BCWResourceManager.instance.MeshLoader(arrTextureChanger[j]));
						}
					}
					if (Device.indexWeakDevice == 0)
					{
						for (int i = 0; i < arrTextureChangerDisabledForWeak.Length; i++)
						{
							if (!arrTextureChangerDisabledForWeak[i].refCountIncreased)
							{
								StartCoroutine(BCWResourceManager.instance.MeshLoader(arrTextureChangerDisabledForWeak[i]));
							}
						}
					}
				}
				else
				{
					for (int l = 0; l < arrTextureChanger.Length; l++)
					{
						if (arrTextureChanger[l] != null && !arrTextureChanger[l].refCountIncreased)
						{
							yield return StartCoroutine(BCWResourceManager.instance.MeshLoader(arrTextureChanger[l]));
						}
					}
					if (Device.indexWeakDevice == 0)
					{
						for (int k = 0; k < arrTextureChangerDisabledForWeak.Length; k++)
						{
							if (!arrTextureChangerDisabledForWeak[k].refCountIncreased)
							{
								yield return StartCoroutine(BCWResourceManager.instance.MeshLoader(arrTextureChangerDisabledForWeak[k]));
							}
						}
					}
				}
			}
			if (listObj != null && listObj.Count > 0 && lod)
			{
				StaticBatchingUtility.Combine(listObj.ToArray(), null);
			}
		}
		if (!lod && ManagerPreloadingSectors.thisScript.isFirstLoading)
		{
			yield return null;
			ManagerPreloadingSectors.thisScript.listFirstLoadingSectors.Remove(this);
			ManagerPreloadingSectors.thisScript.listFirst.Remove(curSectorInfo);
			ManagerPreloadingSectors.thisScript.SectorEndLoading(this);
		}
		resourcesLoaded = true;
	}

	public void ClearMeshes()
	{
		resourcesLoaded = false;
		TextureChanger[] array = arrTextureChanger;
		foreach (TextureChanger textureChanger in array)
		{
			if (textureChanger != null)
			{
				textureChanger.ClearMesh();
			}
		}
		if (Device.indexWeakDevice != 0)
		{
			return;
		}
		TextureChanger[] array2 = arrTextureChangerDisabledForWeak;
		foreach (TextureChanger textureChanger2 in array2)
		{
			if (textureChanger2 != null)
			{
				textureChanger2.ClearMesh();
			}
		}
	}
}
